home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / ups.rexx < prev    next >
OS/2 REXX Batch file  |  2000-11-28  |  3KB  |  116 lines

  1. /* ups.rexx - a very stupid udp ports scanner (but it works...:-) */
  2.  
  3. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  4. if AddLibrary("rexxsupport.library","rxsocket.library","rxlibnet.library")~=0 then exit
  5.  
  6. prg=ProgramName("NOEXT")
  7.  
  8. if ~RMH_ReadArgs("HOST/A,FROM/N,TO/N,T=TIMEOUT/K/N,I=INTERFACE/K") then do
  9.     call PrintFault()
  10.     exit
  11. end
  12.  
  13. if parm.1.flag then from=parm.1.value
  14. else from=1
  15.  
  16. if parm.2.flag then high=parm.2.value
  17. else
  18.     if parm.1.flag then high=from
  19.     else high=100
  20. if from<1 | high>65535 | from>high then call err "bad ports sequence" from"..."high,1
  21.  
  22. if parm.3.flag then secs=parm.3.value
  23. else secs=5
  24. if secs<1 then call err "bad timeout value '"secs"'",1
  25.  
  26. if parm.4.flag then dev=parm.4.value
  27. else do
  28.     dev=suitable()
  29.     if dev="" then call err "no suitable device found",1
  30. end
  31.  
  32. sin.addrAddr=resolve(parm.0.value)
  33. if sin.addrAddr=-1 then call err "host <"parm.0.value"> not found",1
  34. cxaddr=c2x(addr2c(sin.addrAddr))
  35.  
  36. us=socket(inet,dgram)
  37. if us<0 then call err "can't create socket"
  38.  
  39. fstring="icmp[0]=3"
  40. filter=MiamiPCapCompile(fstring,dev)
  41. if filter="" then call err PCAPERR,1
  42.  
  43. s=2**AllocSignal()
  44. pf=MiamiCreatePF(dev,s)
  45.  
  46. if IsDotAddr(parm.0.value)
  47.     then say "Scanning <"parm.0.value"> on '"dev"'" from"..."high
  48.     else say "Scanning <"parm.0.value"> ["sin.addrAddr"] on '"dev"'" from"..."high
  49.  
  50. todo=0
  51. do i=from to high
  52.     sin.addrport=i
  53.     if sendto(us,"D0A"x,,"SIN")<0 then ports.i=0
  54.     else ports.i=1
  55.     todo=todo+ports.i
  56.     if and(SetSignal(0,s),s)>0 then do
  57.         p=MiamiPFNext(pf)
  58.         do while p~=""
  59.             if MiamiPCapMatch(filter,p) then do
  60.                 t=c2d(substr(p,21,1))
  61.                 c=c2d(substr(p,22,1))
  62. /*                addr=substr(p,45,4)
  63.                 if cxaddr~=c2x(addr) then say cxaddr c2x(addr)*/
  64.                 if t=3 & c~=3 then call err "error on icmp packet type:"t "code:"c,1
  65.                 pp=c2d(substr(p,51,2))
  66.                 ports.pp=0
  67.                 todo=todo-1
  68.             end
  69.             p=MiamiPFNext(pf)
  70.         end
  71.     end
  72. end
  73.  
  74. secs=todo%3
  75. if secs<5 then secs=5
  76. say "Sending completed, waiting for" todo "returns for" secs "seconds"
  77.  
  78. tim=CreateTimer()
  79. ts=TimerSignal(tim)
  80. call StartTimer(tim,secs)
  81. sigs=or(s,ts,2**12)
  82.  
  83. do while todo>0
  84.     res=wait(sigs)
  85.     if and(res,s)>0 then do
  86.         p=MiamiPFNext(pf)
  87.         do while p~=""
  88.             if MiamiPCapMatch(filter,p) then do
  89.                 pp=c2d(substr(p,51,2))
  90.                 ports.pp=0
  91.                 todo=todo-1
  92.             end
  93.             p=MiamiPFNext(pf)
  94.         end
  95.     end
  96.     else leave
  97. end
  98.  
  99. do i=from to high
  100.     if ports.i then say i
  101. end
  102. exit
  103.  
  104. err: procedure expose prg
  105. parse arg msg,ntdoerr
  106.     if ntdoerr~=1 then msg=msg "("ErrorString()")"
  107.     say prg":" msg
  108.     exit
  109.  
  110. suitable: procedure
  111.     res=QueryInterfaces("IN")
  112.     do i=0 to res-1
  113.         if in.i.family=2 & in.i.up=1 & in.i.LoopBack=0 then return in.i.name
  114.     end
  115.     return ""
  116.